using System;
using System.Text;

namespace TXK
{
    /// <summary>
    /// 字符串转换类
    /// </summary>
    public class StringLib
    {
        /// <summary>
        /// 将字节数组转换成字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetStringFromByteArrayByBitConvert(byte[] source, int start, int count)
        {
            return BitConverter.ToString(source, start, count);
        }

        /// <summary>
        /// 将字节数组转换成带编码格式字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetStringFromByteArray(byte[] source, int start, int count, Encoding encoding)
        {
            return encoding.GetString(ByteArrayLib.GetByteArray(source, start, count));
        }

        /// <summary>
        /// 将字节数组转换成带编码格式字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetStringFromByteArray(byte[] source, int start, int count)
        {
            return Encoding.ASCII.GetString(ByteArrayLib.GetByteArray(source, start, count));
        }

        /// <summary>
        /// 将字节数组转换成带16进制字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static string GetHexStringFromByteArray(byte[] source, int start, int count, char segment = ' ')
        {
            byte[] byteArray = ByteArrayLib.GetByteArray(source, start, count);
            StringBuilder stringBuilder = new StringBuilder();
            if (byteArray.Length != 0)
            {
                byte[] array = byteArray;
                foreach (byte b in array)
                {
                    if (segment == '\0')
                    {
                        stringBuilder.Append($"{b:X2}");
                    }
                    else
                    {
                        stringBuilder.Append($"{b:X2}{segment}");
                    }
                }
            }
            if (segment != 0 && stringBuilder.Length > 1 && stringBuilder[stringBuilder.Length - 1] == segment)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 将字节数组转换成带16进制字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static string GetHexStringFromByteArray(byte[] source, char segment = ' ')
        {
            return GetHexStringFromByteArray(source, 0, source.Length, segment);
        }

        /// <summary>
        /// 将字节数组转换成西门子字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetSiemensStringFromByteArray(byte[] source, int start, int length)
        {
            byte[] byteArray = ByteArrayLib.GetByteArray(source, start, length + 2);
            int num = byteArray[1];
            if (num > 0)
            {
                return Encoding.GetEncoding("GBK").GetString(ByteArrayLib.GetByteArray(byteArray, 2, num));
            }
            return "empty";
        }

        /// <summary>
        /// 布尔数组转换成字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="IsTrueFormat"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static string GetStringFromBitArray(bool[] source, bool IsTrueFormat = true, char segment = ' ')
        {
            return GetStringFromBitArray(source, 0, source.Length, IsTrueFormat, segment);
        }

        /// <summary>
        /// 布尔数组转换成字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <param name="IsTrueFormat"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static string GetStringFromBitArray(bool[] source, int start, int count, bool IsTrueFormat = true, char segment = ' ')
        {
            bool[] bitArray = BitLib.GetBitArray(source, start, count);
            StringBuilder stringBuilder = new StringBuilder();
            if (bitArray.Length != 0)
            {
                bool[] array = bitArray;
                for (int i = 0; i < array.Length; i++)
                {
                    bool flag = array[i];
                    if (segment == '\0')
                    {
                        if (IsTrueFormat)
                        {
                            stringBuilder.Append(flag.ToString());
                        }
                        else
                        {
                            stringBuilder.Append(flag ? "1" : "0");
                        }
                    }
                    else if (IsTrueFormat)
                    {
                        stringBuilder.Append(flag.ToString() + segment);
                    }
                    else
                    {
                        stringBuilder.Append(flag ? "1" : ("0" + segment));
                    }
                }
            }
            if (segment != 0 && stringBuilder.Length > 1 && stringBuilder[stringBuilder.Length - 1] == segment)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 各种类型数组转换成字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static string GetStringFromValueArray<T>(T[] source, char segment = ' ')
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (source.Length != 0)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    T val = source[i];
                    if (segment == '\0')
                    {
                        stringBuilder.Append(val.ToString());
                    }
                    else
                    {
                        stringBuilder.Append(val.ToString() + segment);
                    }
                }
            }
            if (segment != 0 && stringBuilder.Length > 1 && stringBuilder[stringBuilder.Length - 1] == segment)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            return stringBuilder.ToString();
        }
    }
}
